package com.tamartech.greendb.importers;

import java.io.File;
import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;

import com.Ostermiller.util.CSVParser;
import com.tamartech.greendb.loader.VoteTypeLoader;

// class to initially import Jonathan's spreadsheet. Unlikely ever to need it again
public class VoteTypeImporter
{
    private static Logger logger = Logger.getLogger (VoteTypeImporter.class);
    private static SimpleDateFormat formatRollDate = new SimpleDateFormat ("dd/MM/yyyy");
        
//    private static final int COL_REF1 = 0;
//    private static final int COL_REF2 = 1;
    private static final int COL_REF = 15;
    private static final int COL_VOTE_TYPE = 43; 
    
    public static void main(String[] args) throws Exception
	{
        File inFile = new File (args[0]);
        String wardName = args[1];
        Date rollDate = formatRollDate.parse(args[2]);
        
        VoteTypeLoader loader = new VoteTypeLoader();
        loader.begin(wardName, rollDate);
        
        CSVParser csv = new CSVParser (new FileInputStream (inFile));
        
        
        for (;;)
        {
            // not all cols are labelled
            String[] line = csv.getLine();

            if (line == null)
                break;

            String[] ref = line[COL_REF].trim().split(" ");
            
            String strVoteType = line[COL_VOTE_TYPE].trim();
//            String party2008 = csv.getValueByLabel("08 Party").trim();
            
            String voteType = parseVoteType (strVoteType);
            
            if (voteType != null)
                loader.putVoteType (ref[0], ref[1], voteType);
        }
        
        csv.close();

        loader.end();
	}	
    
    private static String parseVoteType (String s)
    {
        String result = null;
        
        s = s.trim();
        
        if (s.equalsIgnoreCase("All Elections"))
            result = "Postal_All";
        else
        if (s.equalsIgnoreCase("Local Government Elections Only"))
            result = "Postal_Local";
        else
        if (s.equalsIgnoreCase("Parliamentary Elections Only"))
            result = "Postal_Parliamentary";
        else
        if (s.length() > 0)
        {
            logger.error("Invalid vote type value: " + s);
        }
        
        return result;
    }
    
    
} 